<?php
class Wedding{

	function __construct(){
		
	}

	function __destruct(){

	}


    public static function getWeddingScheduleSpecificationByWeddingId($id=NULL){
        $where = " WHERE wss.status != '2' ";
        if(!empty($id)) $where.=" AND wss.wedding_id =".$id;
        $qry = "SELECT wss.*,v.display_name FROM wedding_schedule_specification as wss
        INNER JOIN venues as v ON v.id = wss.venue_id "
        .$where;
        $rs = MyDB::$db->Execute($qry);
        $wedding_schedule = array();
        while ($row = $rs->fetchRow()) {
            $wedding_schedule[] = $row;
        }
        return $wedding_schedule;
    }

    /* This function is used to get all venues
    @Input : interger $id
    @Output : array $venues
    */
    public function getWeddingPackagesById($id = 0){
        $where = " WHERE wp.status != '2' ";
        if(!empty($id)) $where.=" AND wp.id =".$id;
        $qry = "SELECT wp.*, venues.display_name as venue_name, vendor.name as vendor_name FROM wedding_package as wp
        INNER JOIN venues ON venues.id = wp.venue_id
        INNER JOIN vendor ON vendor.id = wp.vendor_id 
         ".$where;
        $rs = MyDB::$db->Execute($qry);
        $wedding_package = array();
        while ($row = $rs->fetchRow()) {
            $wedding_package[] = $row;
        }
        return $wedding_package;
    }
    
	public static function getPackage($parms=null){
    		
        $qry = "select * from wedding_package ";
		if( $parms ){      
             $limitClaus = '';
             if( $parms['paging'] > 0 && $parms['count']<>1 ){
                $perpage = $parms['pagecount'] > 0 ? $parms['pagecount'] : 20;
                $limit =  ($parms['paging']-1)*$perpage;
                $limitClaus = " limit $limit, $perpage ";
             }              
        }
        
        $qry .= $limitClaus;
		$rs = MyDB::$db->Execute($qry);
		if( $rs ){
			if( $parms['count']==1) return $rs->RecordCount();
            
            $arr = $rs->GetArray();
			return $arr;
		}
		return false;
	}             
    
    
    public static function getThemeById($id=null){
        if( $id>0 ){
            $qry = "select * from wedding_theme_master where id=".MyDB::$db->qstr($id)." and status='1'";
            $rs = MyDB::$db->Execute($qry);  
            if( $rs ){
                $arr = $rs->fetchRow();
                return $arr;                
            }
        }else{                                           
            $qry = "select * from wedding_theme_master where status='1'";
            $rs = MyDB::$db->Execute($qry);  
            if( $rs ){
                $arr = $rs->GetArray();
                return $arr;                
            }        
        }
        return false;     
    }
    
    public static function getPackageById($id=null, $condition=null){
        if( $id>0 ){
            $qry = "select * from wedding_package where id=".MyDB::$db->qstr($id);
            if( !empty($condition) ){
                foreach( $condition as $key => $val ){
                    $tmpArr[] = $key .'='. MyDB::$db->qstr($val);                
                }
                $qry = $qry . ' && '.implode(' && ', $tmpArr);
            }
            
            $rs = MyDB::$db->Execute($qry);  
            if( $rs ){
                $arr = $rs->fetchRow();
                return $arr;                
            }
        }
        return false;     
    } 

// Front End Queries	
	public function getWeddingPackage($limit='',$param='') { 
		//MyDB::$db->debug = true;
		if($param['country'])
			$and .= " AND V.country = ".(int)$param['country'];
		if($param['city'])
			$and .= " AND V.city = ".(int)$param['city'];
		if($param['wtheme'])
			$and .= " AND WP.theme_id = ".(int)$param['wtheme'];
		if($param['wcategory'])
			$and .= " AND V.venue_type = ".(int)$param['wcategory'];
		$order_by = ($param['sort_package'] != '')? " ORDER BY WP.".$param['sort_package']:' ORDER BY WP.price ';
		$sql ="SELECT WP.* FROM wedding_package WP LEFT JOIN venues V ON (WP.venue_id = V.id) WHERE WP.status=1 ".$and.$order_by.$limit;
		$rset = MyDB::$db->GetArray($sql);
		
		return $rset;
	}
	
	public function getWeddingPackageCount($param='') { 
		if($param['country'])
			$and .= " AND V.country = ".(int)$param['country'];
		if($param['city'])
			$and .= " AND V.city = ".(int)$param['city'];
		if($param['wtheme'])
			$and .= " AND WP.theme_id = ".(int)$param['wtheme'];
		if($param['wcategory'])
			$and .= " AND V.venue_type = ".(int)$param['wcategory'];
		$sql ="SELECT count(*) FROM wedding_package WP LEFT JOIN venues V ON (WP.venue_id = V.id) WHERE WP.status='1' ".$and;
		return MyDB::$db->GetOne($sql);
	
	}
	
	public function getWeddingPackageDetail($param=''){
		MyDB::$db->SetFetchMode(ADODB_FETCH_ASSOC);
		$query = "SELECT WP.*, V.venue_name, V.vendor_id, V.venue_name, V.address, V.city, V.state, V.country, V.landmark, V.min_capacity venue_min_capacity,V.max_capacity venue_max_capacity FROM wedding_package WP LEFT JOIN venues V ON (WP.venue_id = V.id) WHERE WP.status=1 AND package_url='".$param['package_url']."'";
		return MyDB::$db->GetRow($query);
	}
	
	public function getWeddingPackageSpecification($param=''){
		MyDB::$db->SetFetchMode(ADODB_FETCH_ASSOC);
		$query = "SELECT * FROM wedding_schedule_specification where wedding_id=".(int)$param['id']." and status='1'";
		return MyDB::$db->GetArray($query);
		
	}
	
	public function getWeddingPackageServices($ids=''){
		
		MyDB::$db->SetFetchMode(ADODB_FETCH_ASSOC);
		$query = "SELECT vs.*, sm.name AS service_name, v.name vendor_name FROM vendor_services AS vs INNER JOIN vendor AS v ON(vs.vendor_id = v.id) INNER JOIN services_master AS sm ON(vs.service_id = sm.id) WHERE vs.id IN(".$ids.") AND sm.status='1' AND v.status='1'";
		
		
		return MyDB::$db->GetArray($query);
	}
	
	
	public function getWeddingTips($count=1,$id='') {
		
		MyDB::$db->SetFetchMode(ADODB_FETCH_ASSOC);
		if($id)
			$and = " AND id = ".(int)$id;
		$query = "SELECT * FROM wedding_tips where status=1 $and LIMIT $count";
		return MyDB::$db->GetArray($query);
		
	}
	
	public function getWeddingCategories($id='') {
		
		MyDB::$db->SetFetchMode(ADODB_FETCH_ASSOC);
		if($id)
			$and = " AND id = ".(int)$id;
		$query = "SELECT * FROM wedding_categories where status=1 ".$and;
		return MyDB::$db->GetArray($query);
		
	}
	
	public function getWeddingPackageRandom($id=NULL,$count=1) {
		
		MyDB::$db->SetFetchMode(ADODB_FETCH_ASSOC);
		$query = "SELECT * FROM wedding_package where id !=".(int)$id." order by rand() LIMIT $count";
		return MyDB::$db->GetArray($query);
		
	}
	
	public function calcPrice($arr){
		if($arr['price_type'] == 2)
			$price = $arr['price'];
		if($arr['price_type'] == 1)
			$price = $arr['price'];
		if($arr['discount_type'] == 2)
			$discount = ($price*$arr['discount'])/100;
		elseif($arr['discount_type'] == 1)
			$discount = $arr['discount'];
		$price = $price-$discount;
		$price = ($price > 0)?$price:0;
		return number_format($price,2);
	}
	  
}
?>